package betfc

import (
	"net/http"
	"strings"
	"io/ioutil"
	"time"
	"strconv"
	"net/url"
	"tidwall/gjson"
	"betdata"
	"common"
)

//幸运农场 无封盘时间 二十四小时畅玩
func GetNcBetMap() {
	defer GetNcBetMap()
	betMap, sleepTime,period := NcServer()
	if sleepTime >= 0 {
		time.Sleep(time.Duration(sleepTime+1) * time.Second)
		return
	} else if sleepTime < 0 {
		betdata.WrightFile(period,"nc",betMap)    //todo 将数据写入到对应的配置文件中
		time.Sleep(time.Duration(140) * time.Second)     //封盘倒计时
		return
	}
}

//玩法参数
type PlayNcStruct struct {
	Handicap    string `json:"handicap"`
	BuhuoStatus string `json:"buhuoStatus"`
	Play        string `json:"play"`
}

//幸运农场 无封盘时间 todo 接收cookie并替换
func NcServer() (map[string]int, int,string) {
	betMap := make(map[string]int)
	var sleeptime int
	var period string
	playData := []PlayNcStruct{
		{
			Handicap:    "A",
			BuhuoStatus: "1",
			Play:        "sumDT",
		}, {
			Handicap:    "A",
			BuhuoStatus: "1",
			Play:        "ball1",
		}, {
			Handicap:    "A",
			BuhuoStatus: "1",
			Play:        "ball2",
		}, {
			Handicap:    "A",
			BuhuoStatus: "1",
			Play:        "ball3",
		}, {
			Handicap:    "A",
			BuhuoStatus: "1",
			Play:        "ball4",
		}, {
			Handicap:    "A",
			BuhuoStatus: "1",
			Play:        "ball5",
		}, {
			Handicap:    "A",
			BuhuoStatus: "1",
			Play:        "ball6",
		}, {
			Handicap:    "A",
			BuhuoStatus: "1",
			Play:        "ball7",
		}, {
			Handicap:    "A",
			BuhuoStatus: "1",
			Play:        "ball8",
		},
	}
	for _, val := range playData {      //todo 循环获取每个玩法的下注情况 sysinfo=nc%7C0%7Ca%7C0%7C0;
		q := url.Values{}
		q.Set("handicap", val.Handicap)
		q.Set("buhuoStatus", val.BuhuoStatus)
		q.Set("Play", val.Play)
		//body := ioutil.NopCloser(strings.NewReader(q.Encode()))
		client := &http.Client{}
		tUnix := time.Now().UnixNano() / 1e6
		strAjax := strconv.Itoa(int(tUnix)) + "__ajax"
		req, _ := http.NewRequest("POST", "http://tc6.x.l11ll.ninja/sscat35657c_232/nc/monitor/ajax/?&_="+strAjax, strings.NewReader(q.Encode()))

		cookieString := common.GetCookies()
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
		for _,value := range cookieString {
			req.AddCookie(&value)
		}
		ck:=new(http.Cookie)
		ck.Name = "sysinfo"
		ck.Value = "nc%7C0%7Ca%7C0%7C0"
		req.AddCookie(ck)
		resp, _ := client.Do(req)
		defer resp.Body.Close()
		result, _ := ioutil.ReadAll(resp.Body)
		arr := strings.Split(string(result), "êêê") //切割字符串

		state := gjson.Get(arr[0], "data.betnotice.timeclose") //封盘倒计时
		timesnow := gjson.Get(arr[0], "data.betnotice.timenow") //期数
		betData := gjson.Get(arr[0], "data.supervision")       //下注内容
		period = timesnow.Str
		sleeptime = int(state.Int())
		if sleeptime > 0 { //未封盘
			return betMap, sleeptime,period
		} else if sleeptime < 0 {
			betRange := betData.Map()
			for k, v := range betRange {
				temp := v.Raw
				temp= strings.Replace(temp,"\"","",-1)
				arr := strings.Split(temp, ",")
				betNum, _ := strconv.Atoi(arr[1])
				if betNum > 0 {
					betMap[k] = betNum
				}
			}
		}
	}
	return betMap, sleeptime,period
}
